import {
  fileUploadHandle,
} from "@utils";
export default {

    downloadFile({ name, url } = {}) {  //下载模板
        if (!url) {
          return;
        }
        name = name || url.substring(url.lastIndexOf("/") + 1);
        let tagA = document.createElement("a");
        tagA.setAttribute("download", name);
        tagA.setAttribute("href", url);
        document.body.appendChild(tagA);
        tagA.click();
        document.body.removeChild(tagA);
    },
    uploadFile(serviceName,params,callback) { //自动上传
      
     fileUploadHandle({
          serviceName: serviceName,
          accept: ".xls,.xlsx,application/vnd.ms-excel",
          params,
          onRequestSuccess: (data) => {
            callback&&callback(data)
          },
          onSuccess: (data) => {
            
            
           
          },
          onError: (data) => {
             
          },
          onProcess: (data) => {
             
          },
      });
     
    },
    goback(vm,name){ // this.$tool.goback(this,'exampagerManage') 返回
    
        let navTabList=vm.$store.state.global.navTabList
        let currname=vm.$route.name
        let index=null
        if(!name){ //返回上一级
            
            let fullPath=vm.$route.fullPath
            index=navTabList.findIndex(item=>{
                return item.fullPath==fullPath
            })
            if(index>=1){
                let skipUrl=navTabList[index-1].fullPath;
                navTabList.splice(index,1)
                //let option=navTabList[index-1]
                //window.pushRoute(option)
               vm.$router.push({path:skipUrl});
            }
        }else{ //返回指定组件
            index=navTabList.findIndex(item=>{
                return item.name==currname
            })
            navTabList.splice(index,1)
            vm.$router.push({name});
        }
        vm.$store.commit("global/SET_NAV_TAB_LIST", navTabList)
      
    },
    repeatArry(arr,id){  //去重
        let hash = {}
        let newreatArry=arr.reduce((item, next) => {
            hash[next[id]] ? '' : hash[next[id]] = true && item.push(next)
            return item
        }, [])

        return newreatArry
    },
    sortArr(arr, str) { //数组对象含有相同的元素值，重新组装成一个新数组。sortArr( arr, 'name');
        var _arr = [],
            _t = [],
            _tmp;
        arr = arr.sort(function(a, b) {
            var s = a[str],
                t = b[str];

            return s < t ? -1 : 1;
        });
     
        if ( arr.length ){
            _tmp = arr[0][str];
        }
        for (var i in arr) {
            if ( arr[i][str] === _tmp ){
                _t.push( arr[i] );
            } else {
                _tmp = arr[i][str];
                _arr.push( _t );
                _t = [arr[i]];
            }
        }
      
        _arr.push( _t );
        return _arr;
    },
    forbitFun(){
        document.oncontextmenu = new Function("return false;");
        document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && (e.keyCode == 123 || e.keyCode == 116)) {
                    e.returnValue = false;
                    return (false);
            }
        }
    },
    alertFun(vm,msg){
        vm.$alert(msg, '提示', {
           confirmButtonText: '确定',
           callback: action => {
           }
       });
   },
   confirmFun(vm,msg,callback){

    vm.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         
        callback(params)
        console.log(callback,'callback')
      }).catch(() => {
           
      });

   },
  
    getBaseInfo(){
        return JSON.parse(sessionStorage.getItem('userInfo'));
    },
    getCurrentGroupId(){

        let groupIdList=JSON.parse(sessionStorage.getItem('currentGroupId'))
        
        return  groupIdList.map(item=>{return item.groupId}) 
    },
    sortFun(arr,id){ //排序
        arr.sort(function(a,b){
            return a[id]-b[id]
        })
        return arr;
    },
    // 同一个数组之内数据，转化父子关系
    translateDataToTree(data,{parentId='groupId',id='id'}) {
                // let list=[]
                // data[0].id='1'
                // delete data[0].groupId
                // let newList=data
                // let newArry=this.$tool.translateDataToTree(newList,{parentId:'groupId',id:'id'})
                // this.treeData = newArry;
            const treeData = [];
            for (let i = 0; i < data.length; i++) {
                let ele = data[i];
                if (!ele[parentId]) {
                    treeData.push(ele); // ele为根节点时，push到treeData中
                    continue;
                }
                translateChildren(treeData, ele);
            }
            function translateChildren(arr, ele) {
                arr.forEach(item => {
                    if (ele[parentId] == item[id]) {
                        if (!item.children) item.children = [];
                        item.children.push(ele);
                    }
                    if (item.children) {
                        translateChildren(item.children, ele); // 递归遍历
                    }
                })
            }
            return treeData;
    },
    stopShortCutKey() {               //屏蔽鼠标右键、Ctrl+n、shift+F10、F5刷新、退格键
        if ( (window.event.altKey) && ((window.event.keyCode == 37) ||   (window.event.keyCode == 39) ) ) {
          return false;
        }
        if ((event.keyCode == 116) ||                                   //屏蔽   F5   刷新键
            (event.keyCode == 112) ||                                   //屏蔽   F1   刷新键
            (event.ctrlKey && event.keyCode == 82)) {                   //Ctrl   +   R
             return false;
        }
        if ((event.ctrlKey) && (event.keyCode == 78)){
          return false;      //屏蔽   Ctrl+n
        }

        if ((event.shiftKey) && (event.keyCode == 121)) {  //屏蔽   shift+F10
          return false;
        }
        if (window.event.srcElement.tagName == "A" && window.event.shiftKey){
          return false;        //屏蔽   shift 加鼠标左键新开一网页
        }
        if ((window.event.altKey) && (window.event.keyCode == 115)) {   //屏蔽Alt+F4
          window.showModelessDialog("about:blank", "", "dialogWidth:1px;dialogheight:1px");
          return false;
        }
        if ((window.event.altkey) && (window.event.keyCode == 27)) {
          return false;
        }
  },
  parseNumber: function parseNumber(number, append) {
    append = append || '';
    return (number || "").replace(/^(860|\+860|\+86|86|0086|\+00|00|0|\+0|\b)/, append);
  },
  safePhone: function (phone) {
    if(!phone) return phone;
    return phone.replace(/^(.{3}).+(.{4})$/, "$1****$2");
  }
}